package org.dromara.system.controller.system;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.TradingRecordsBo;
import org.dromara.system.domain.bo.TradingRecordsBySnBo;
import org.dromara.system.domain.vo.TradingRecordsVo;
import org.dromara.system.service.ITradingRecordsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 交易记录
 *
 * @author Lion Li
 * @date 2025-07-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/records")
public class TradingRecordsController extends BaseController {

    private final ITradingRecordsService tradingRecordsService;

    /**
     * 查询交易记录列表
     */
//    @SaCheckPermission("system:records:list")
    @GetMapping("/list")
    public TableDataInfo<TradingRecordsVo> list(TradingRecordsBo bo, PageQuery pageQuery) {
        return tradingRecordsService.queryPageList(bo, pageQuery);
    }


    //    @SaCheckPermission("system:records:listBySn")
    @GetMapping("/listBySn")
    public TableDataInfo<TradingRecordsVo> listBySn(TradingRecordsBySnBo bo, PageQuery pageQuery) {
        return tradingRecordsService.queryPageListBySn(bo, pageQuery);
    }


    /**
     * 导出交易记录列表
     */
//    @SaCheckPermission("system:records:export")
    @Log(title = "交易记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TradingRecordsBo bo, HttpServletResponse response) {
        List<TradingRecordsVo> list = tradingRecordsService.queryList(bo);
        ExcelUtil.exportExcel(list, "交易记录", TradingRecordsVo.class, response);
    }

    /**
     * 获取交易记录详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:records:query")
    @GetMapping("/{id}")
    public R<TradingRecordsVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(tradingRecordsService.queryById(id));
    }

    /**
     * 新增交易记录
     */
//    @SaCheckPermission("system:records:add")
    @Log(title = "交易记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TradingRecordsBo bo) {
        return toAjax(tradingRecordsService.insertByBo(bo));
    }

    /**
     * 修改交易记录
     */
//    @SaCheckPermission("system:records:edit")
    @Log(title = "交易记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TradingRecordsBo bo) {
        return toAjax(tradingRecordsService.updateByBo(bo));
    }

    /**
     * 删除交易记录
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:records:remove")
    @Log(title = "交易记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tradingRecordsService.deleteWithValidByIds(List.of(ids), true));
    }
}
